home *** CD-ROM | disk | FTP | other *** search
/ PC Open 93 / PC Open 93 CD 2.bin / PDF / webdeveloper / lezione_4 / inseriscifoto_risposta_con_controlli.asp < prev    next >
Encoding:
Text File  |  2003-12-03  |  2.7 KB  |  103 lines

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2.  
  3. <html>
  4. <head>
  5.     <title>Gestione foto di Mario Rossi</title>
  6. </head>
  7.  
  8. <body>
  9.  
  10. <h1>Inserimento nuova foto</h1>
  11.  
  12. <%
  13.  
  14. Const adOpenForwardOnly = 0
  15. Const adLockReadOnly = 1
  16.  
  17. Dim contatore
  18. Dim sql1,sql2
  19. Dim conn, rs
  20. Dim i,j
  21.  
  22. Dim strNome, strTitolo, strData, strLuogo, strProvincia, strStato
  23.  
  24. strNome = request.Form("nome")
  25. strTitolo = request.Form("titolo")
  26. strData = request.Form("datagg") & "/" & request.Form("datamm") & "/" & request.Form("datayyyy")
  27. strLuogo = request.Form("luogo")
  28. strProvincia = request.Form("provincia")
  29. strStato = request.Form("stato")
  30.  
  31. Set conn = Server.CreateObject("ADODB.Connection")
  32. conn.Open "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & server.MapPath("/mdb-database/foto2.mdb")
  33.  
  34. sql = "SELECT count(*) AS totale FROM Foto WHERE nome = '" & request.Form("nome") & "'"
  35.  
  36. Set rs = Server.CreateObject("ADODB.RecordSet")
  37. rs.Open sql, conn, adOpenForwardOnly, adLockReadOnly
  38.  
  39. 'Qualche controllo
  40.  
  41. If rs("totale") > 0 Then
  42.     'Esiste una foto giα inserita con lo stesso ID
  43.     response.write "Questo nome di foto esiste giα nella tabella, devi sceglierne un altro<br>"
  44.     response.write "Ritorna alla<a href=""inseriscifoto.asp"">pagina di inserimento</a>"
  45.     response.end
  46. End If
  47.  
  48. If Not IsDate(strData) Then
  49.     'Data in formato non valido
  50.     response.write "La data inserita (" & strData & ") non Φ valida<br>"
  51.     response.write "Ritorna alla<a href=""inseriscifoto.asp"">pagina di inserimento</a>"
  52.     response.end
  53. End If
  54.  
  55. 'Rinforzo l'accento per evitare problemi in inserimento
  56. strNome   = replace(strNome,"'","''")
  57. strTitolo = replace(strTitolo,"'","''")
  58. strLuogo = replace(strLuogo,"'","''")
  59.  
  60. sql = "INSERT INTO Foto ( Nome, Titolo, Data, Luogo, IdProvincia, IdStato ) VALUES (" & _
  61.     "'" & strNome & "', " & _
  62.     "'" & strTitolo & "', " & _
  63.     "'" & strData & "', " & _
  64.     "'" & strLuogo & "', " & _
  65.     "'" & strProvincia & "', " & _
  66.     "'" & strStato & "' " & _
  67.     ")"
  68.  
  69. on error resume next
  70.  
  71. conn.Execute sql
  72.  
  73. If ConnError(conn) > 0 Then
  74.     response.write "Si Φ verificato un problema con l'inserimento dei dati.<br>"
  75.     response.write "Ritorna alla <a href=""inseriscifoto.asp"">pagina di inserimento</a>"
  76. Else
  77.     response.write "I dati della foto sono stati inseriti correttamente<br>"
  78.     response.write "Ritorna all'<a href=""gestione.asp"">elenco delle foto</a>"
  79. End If
  80.  
  81. on error goto 0
  82.  
  83. set rs = nothing
  84. conn.close
  85. set conn = nothing
  86.  
  87. Function ConnError(conn)
  88.  
  89.   Dim i
  90.   i = 0
  91.  
  92.   For Each errorObject In conn.Errors
  93.       i = i + 1
  94.     Response.Write "ERRORE: " & errorObject.Description & "<br><br>"
  95.   Next
  96.  
  97.   ConnError = i
  98.  
  99. End Function
  100.  
  101. %>
  102.  
  103.